package cn.lzj.controller.base;

import cn.lzj.Utils.AliSmsUtil;
import cn.lzj.pojo.User;
import cn.lzj.service.base.RegisterService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class registerController {

    @Resource
    private RegisterService registerService;

    String code = "";

    @GetMapping("/register")
    public String registerIndex(){
        return "register";
    }

    @PostMapping("/registerForm")
    public String registerForm(String username, String accountNo, String firstPassword,
                               String telephone, String check, String email, Model model){
        if(code.equals(check)){
            //创建对象
            User user = new User(null, username, accountNo, firstPassword, telephone, "null", email, "user");
            registerService.registerUser(user);
            //注册成功回到登录页面
            return "login";
        }
        //注册失败回到注册页面
        model.addAttribute("check","visible");
        return "register";
    }


    @GetMapping("/getCodeNum/{phone}")
    @ResponseBody
    public String getCodeNum(@PathVariable("phone") String phone,Model model){
        //通过redis去获取验证码
        code = registerService.getCodeNum(phone);
        System.out.println(code);
        AliSmsUtil.sendSmsCode(phone,code);
        return "获取成功";
    }

}
